﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.Common;
using YwangHRMSys.DBUtility;
using YwangHRMSys.Model;


public class ContractInfoDAL
    {
        #region 基础方法

        #region 开始生成tomodel提高代码的重用性

        /// 开始生成tomodel提高代码的重用性
        private ContractInfoModel ToModel( DataRow row )
        {
            var model = new ContractInfoModel();
            model.Id = (Guid) SqlHelper.FromDbValue(row["Id"]);
            model.ContractSigned = (DateTime) SqlHelper.FromDbValue(row["ContractSigned"]);
            model.ContractExpires = (DateTime) SqlHelper.FromDbValue(row["ContractExpires"]);
            model.ContractLength = (String) SqlHelper.FromDbValue(row["ContractLength"]);
            model.EmployeeSiged = (String) SqlHelper.FromDbValue(row["EmployeeSiged"]);
            model.EmployeeExpires = (String) SqlHelper.FromDbValue(row["EmployeeExpires"]);
            model.EmployeeNameId = (Guid) SqlHelper.FromDbValue(row["EmployeeNameId"]);
            model.ContractRemarks = (String) SqlHelper.FromDbValue(row["ContractRemarks"]);
            model.IsRenewed = (bool) SqlHelper.FromDbValue(row["IsRenewed"]);
            return model;
        } //tomodel的结束。

        #endregion

        #region listAll开始

        //查询所有ContractInfo的数据列表
        public IEnumerable <ContractInfoModel> ListAll()
        {
            var list = new List <ContractInfoModel>();
            DataTable table = SqlHelper.ExecuteDataTable
                    (
                            "Select Id,ContractSigned,ContractExpires,ContractLength,EmployeeSiged,EmployeeExpires,EmployeeNameId,ContractRemarks,IsRenewed  FROM  ContractInfo where IsEmployeeDeleted=0");
            foreach (DataRow row in table.Rows)
                {
                    ContractInfoModel model = ToModel(row);
                    list.Add(model);
                }
            return list;
        } //listAllContractInfo结束

        #endregion

        #region  根据ID来查询ContractInfo的信息

        public ContractInfoModel GetByContractInfoID( Guid Id )
        {
            DataTable table = SqlHelper.ExecuteDataTable
                    (
                            " SELECT Id,ContractSigned,ContractExpires,ContractLength,EmployeeSiged,EmployeeExpires,EmployeeNameId,ContractRemarks,IsRenewed,IsEmployeeDeleted  FROM  ContractInfo  WHERE  Id = @Id",
                            new SqlParameter("@Id", Id));
            if (table.Rows.Count <= 0)
                {
                    return null;
                }
            if (table.Rows.Count == 1)
                {
                    return ToModel(table.Rows[0]);
                }
            throw new Exception();
        }

        #endregion

        #region  根据ID修改ContractInfo的信息

        public void UpdateContractInfo( ContractInfoModel model )
        {
            SqlHelper.ExecuteNonQuery
                    (
                            "UPDATE  ContractInfo SET  Id = @Id,ContractSigned = @ContractSigned,ContractExpires = @ContractExpires,ContractLength = @ContractLength,EmployeeSiged = @EmployeeSiged,EmployeeExpires = @EmployeeExpires,ContractRemarks =@ContractRemarks ,IsRenewed=0  WHERE  Id = @Id",
                            new SqlParameter("@Id", model.Id),
                            new SqlParameter("@ContractSigned", model.ContractSigned),
                            new SqlParameter("@ContractExpires", model.ContractExpires),
                            new SqlParameter("@ContractLength", model.ContractLength),
                            new SqlParameter("@EmployeeSiged", model.EmployeeSiged),
                            new SqlParameter("@EmployeeExpires", model.EmployeeExpires),
                            new SqlParameter("@ContractRemarks", model.ContractRemarks)
                    );
        }

        #endregion

        #region 添加ContractInfo信息

        // 添加一条ContractInfo 的信息
        public void InsertContractInfo( ContractInfoModel model )
        {
            SqlHelper.ExecuteNonQuery
                    (
                            @"INSERT INTO ContractInfo  (Id,ContractSigned,ContractExpires,ContractLength,EmployeeSiged,EmployeeExpires,EmployeeNameId,ContractRemarks,IsRenewed,IsEmployeeDeleted   ) values (@Id,@ContractSigned,@ContractExpires,@ContractLength,@EmployeeSiged,@EmployeeExpires,@EmployeeNameId,@ContractRemarks,0,0 )",
                            new SqlParameter("@Id", model.Id),
                            new SqlParameter("@ContractSigned", model.ContractSigned),
                            new SqlParameter("@ContractExpires", model.ContractExpires),
                            new SqlParameter("@ContractLength", model.ContractLength),
                            new SqlParameter("@EmployeeSiged", model.EmployeeSiged),
                            new SqlParameter("@EmployeeExpires", model.EmployeeExpires),
                            new SqlParameter("@EmployeeNameId", model.EmployeeNameId),
                            new SqlParameter("@ContractRemarks", model.ContractRemarks)
                    );
        }

        #endregion

        //根据ID来查询ContractInfo的信息

        //根据ID修改ContractInfo的信息

        #endregion

        #region 扩展方法

        #region 合同续签

        public void UpdateIsRenewed(ContractInfoModel model)
        {

            SqlHelper.ExecuteNonQuery
                    (
                            "UPDATE  ContractInfo SET ContractSigned = @ContractSigned,ContractExpires = @ContractExpires,ContractLength = @ContractLength,EmployeeSiged = @EmployeeSiged,EmployeeExpires = @EmployeeExpires,ContractRemarks =@ContractRemarks ,IsRenewed=1  WHERE  Id = @Id",
                            new SqlParameter("@Id", model.Id),
                            new SqlParameter("@ContractSigned", model.ContractSigned),
                            new SqlParameter("@ContractExpires", model.ContractExpires),
                            new SqlParameter("@ContractLength", model.ContractLength),
                            new SqlParameter("@EmployeeSiged", model.EmployeeSiged),
                            new SqlParameter("@EmployeeExpires", model.EmployeeExpires),
                            new SqlParameter("@ContractRemarks", model.ContractRemarks)
                    );
        }


        #endregion



        #region    软删除
        /// <summary>
    /// 软删除
    /// </summary>
    /// <param name="id"></param>
        public void DeleteById(Guid id)
        {
          SqlHelper.ExecuteNonQuery("UPDATE dbo.ContractInfo SET IsEmployeeDeleted=1 WHERE EmployeeNameId=@Id", new SqlParameter("@Id", id));
          //throw  new Exception(o.ToString());
        }
        #endregion
        #endregion
    }

//当前ContractInfoDAL类结束